package com.SmartCare.utils;

import com.SmartCare.common.dto.medicine.MedicationListDTO;
import com.SmartCare.mapper.medicine.MedicationFrequencyMapper;
import com.SmartCare.mapper.medicine.MedicationMapper;
import com.SmartCare.pojo.medicine.Medication;
import com.SmartCare.pojo.medicine.MedicationList;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 将药单项转成dto的工具类
 */
@Service
public class MedicationListTransToDTO {
    @Resource
    MedicationFrequencyMapper medicationFrequencyMapper;

    @Resource
    MedicationMapper medicationMapper;

    @Resource
    MedicationTransToDTO medicationTransToDTO;

    /**
     * 将MedicationList对象转为MedicationListDTO的工具
     *
     * @param l 药单项对象
     * @return 返回转换后的dto对象
     */
    public MedicationListDTO transToDTO(MedicationList l) {
        MedicationListDTO dto = new MedicationListDTO();
        //获取药单项的用药频率和药品信息
        Medication medication = medicationMapper.selectById(l.getMedicationId());
        //设置药品
        if (medication != null) {
            dto.setMedication(medicationTransToDTO.trans(medication));
        } else {
            dto.setMedication(null);
        }
        //设置频率
        dto.setFrequency(medicationFrequencyMapper.selectById(l.getFrequencyId()));
        //将一些其他属性添加上
        dto.setId(l.getId());
        dto.setElderlyId(l.getElderlyId());
        dto.setNurseId(l.getNurseId());
        dto.setStartTime(l.getStartTime());
        dto.setEndTime(l.getEndTime());
        dto.setListNum(l.getListNum());
        dto.setDosage(l.getDosage());
        dto.setUnit(l.getUnit());
        dto.setType(l.getType());
        dto.setMode(l.getMode());
        //设置时效
        if (l.getTerm() == null) {
            dto.setTerm(null);
        } else {
            dto.setTerm(l.getTerm() ? "长期" : "短期");
        }
        //设置服用时间
        dto.setTakeTime(l.getTakeTime());
        return dto;
    }
}
